﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookShopSample
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
         private void btOk_Click(object sender, EventArgs e)
        {
            int id = 9;
            string name = "sad";
            using (SqlConnection con = new SqlConnection())
            {
                int maxID = 0;
                con.ConnectionString = @"Server=PD-01\SQLEXPRESS;Database=book_shop;Trusted_Connection=True;";
                con.Open();
                string maxElementSqlQuerry = "SELECT * FROM category";
                SqlCommand myCommandMaxElement = new SqlCommand(maxElementSqlQuerry, con);


                using (SqlDataReader strReader = myCommandMaxElement.ExecuteReader())
                {
                    while (strReader.Read())
                    {
                        string s = strReader["id"].ToString();
                        maxID = Convert.ToInt32(s);
                    }
                }
                id = maxID + 1;
                name = tbAdd.Text;
                string strSQL = string.Format("INSERT INTO category (id, name) VALUES ({0}, '{1}')", id, name);
                SqlCommand myCommand = new SqlCommand(strSQL, con);
                myCommand.ExecuteNonQuery();
                this.Close();
            }




        }

    }
}
